﻿<%@ Page MasterPageFile="~/Site.Master" Language="C#" AutoEventWireup="true" CodeBehind="CartView.aspx.cs" Inherits="RabbitShop.UI.CartView" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    
    <form id="form1" action="../Action/CartSet.aspx" method="POST">

    <script type="text/javascript">
        function updatenum(type,inputname) {
            var qty = parseInt(document.forms['form1'].elements[inputname].value);
            if (type == 'sub') {
                if (qty > 1) {
                    document.forms['form1'].elements[inputname].value = (qty - 1);
                    
                }
            } else {
                document.forms['form1'].elements[inputname].value = (qty + 1);
            }
        }
        function getCookie(c_name) {
            if (document.cookie.length > 0) {
                c_start = document.cookie.indexOf(c_name + "=");
                if (c_start != -1) {
                    c_start = c_start + c_name.length + 1;
                    c_end = document.cookie.indexOf(";", c_start);
                    if (c_end == -1) c_end = document.cookie.length;
                    return unescape(document.cookie.substring(c_start, c_end));
                }
            }
            return "";
        }
        function SubmitCart() {
            var uid = getCookie("uid");
            if (uid == "") {
                window.location = "UserLogon.aspx";
                return;
            }
            var data = JSON.parse('<%=cartJson %>');
            for (var i = 0; i < data.length; i++) {
                var inputname = "quantity" + i;
                data[i].Quantity = document.forms['form1'].elements[inputname].value;
            }

            var xhr = new XMLHttpRequest();
            xhr.open(form1.method, form1.action, true);
            xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');

            xhr.send(JSON.stringify(data));

            xhr.onloadend = function () {
                if (xhr.responseText == "true") {
                    window.location = "ConfirmOrder.aspx";
                } else {
                    alert(xhr.responseText);
                }
            };
        }
    </script>
    
    <section class="contact">
        <header>
            <h3>Cart:</h3>
        </header>

        <%= list.Count %><br />
            
        <table style="text-align: center;">
            <tr>
                <td>商品</td>
                <td>名称</td>
                <td>标题</td>
                <td>单价</td>
                <td>数量</td>
                <td>小计</td>
                <td>操作</td>
            </tr>

            <asp:Repeater runat="server" ID="myRepeater"  >
                <ItemTemplate>
                    <tr>
                        <td>
                            <a href="ProductDetail.aspx?id=<%#Eval("ItemInfo.ProductId") %>" target="_blank" >
                                <img alt="产品图片" width="50" src="../Images/accent.png" />
                            </a>
                        </td>
                        <td style="width: 50px;">
                            <%#Eval("ProductInfo.Name") %>
                        </td>
                        <td style="width: 200px;">
                            <div>
                                <%#Eval("ProductInfo.Title") %>
                            </div>
                            <div>
                                <%#Eval("ProductInfo.SubTitle") %>
                            </div>
                        </td>
                        <td>
                            <%#Eval("ProductInfo.PricePerUnit") %>
                        </td>
                        <td>
                            <a onclick="updatenum('sub','quantity<%#Container.ItemIndex %>');" href="javascript:void(0);">-</a>
                            <input type="text" value="<%#Eval("ItemInfo.Quantity") %>" name="quantity<%#Container.ItemIndex %>" style="width: 30px; text-align: right;"/>
                            <a onclick="updatenum('add','quantity<%#Container.ItemIndex %>');" href="javascript:void(0);">+</a> 
                                
                        </td>
                        <td>
                            <%#(decimal)Eval("ProductInfo.PricePerUnit") * (int)Eval("ItemInfo.Quantity") %>
                        </td>
                        <td>
                            <a href="/Action/CartItemDelete.aspx?oid=<%#Eval("ItemInfo.ProductId") %>">删除</a>
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </table>
            
        <div style="width: 173px;background: #f40 none repeat scroll 0 0;
border-color: #f40;

border-radius: 2px;
border-style: solid;
border-width: 1px;
cursor: pointer;
display: block;
font-family: Hiragino Sans GB,microsoft yahei,sans-serif;
font-size: 16px;
height: 38px;
line-height: 38px;
text-align: center;">
            <a data-spm-click="gostr=/tbdetail;locaid=d2"  class="J_LinkAdd" title="加入购物车" href="javascript:SubmitCart();"
                style="color: #fff;" >
                <i class="tb-iconfont" style=" font-size: 18px;
font-style: normal;
font-weight: 400;
margin-right: 10px;
vertical-align: bottom;">ŭ</i>
                结算
            </a>
        </div>
    </section>

    </form>
</asp:Content>
